/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.tools;

import java.awt.event.ActionEvent;
import java.util.Set;

import dgbgui.DGBGUI;
import dgbgui.command.Command;
import dgbgui.command.CommandStack;
import dgbgui.command.RelabelQueryCommand;
import dgbgui.gui.dialogs.RenameDialog;
import dgbgui.modeldocument.Query;
import dgbgui.selection.SelectionManager;

public class RenameTool extends DialogedTool {

    public void execute() {

        Set<Object> queries = SelectionManager.getInstance().getSelection(Query.class);
        assert ( queries.size() == 1);
        m_query = (Query)(queries.iterator().next());

        m_dialog = new RenameDialog(this,m_query);
        m_dialog.pack();
        DGBGUI.centerDialog(m_dialog);
        m_dialog.setVisible(true);

    }

    public void actionPerformed(ActionEvent event) {
        if ( event.getSource() == m_dialog.getOkButton() ) {

            // Update all the properties
            String newLabel = ((RenameDialog)m_dialog).getName();

            // Replace/create
            Command command;
            command = new RelabelQueryCommand(m_query,newLabel);
            CommandStack.getInstance().execute(command);
        }
        

        // Close the tool
        doClose();
    }


    private Query m_query;

}
